<?php

namespace addons\department;

use app\common\library\Menu;
use think\Addons;
use think\Db;
use think\Lang;
use think\Loader;

/**
 * 插件
 */
class Department extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = [];
        $config_file = ADDON_PATH . "department" . DS . 'config' . DS . "menu.php";
        if (is_file($config_file)) {
            $menu = include $config_file;
        }
        if ($menu) {
            Menu::create($menu);
        }
        $this->addField();

        return true;


    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        $info = get_addon_info('department');
        Menu::delete(isset($info['first_menu']) ? $info['first_menu'] : 'department');
        return true;
    }

    /**
     * 插件启用方法
     */
    public function enable()
    {
        $info = get_addon_info('department');
        Menu::enable(isset($info['first_menu']) ? $info['first_menu'] : 'department');
    }

    /**
     * 插件禁用方法
     */
    public function disable()
    {
        $info = get_addon_info('department');
        Menu::disable(isset($info['first_menu']) ? $info['first_menu'] : 'department');
    }

    /**
     * 插件升级方法
     */
    public function upgrade()
    {
        $this->addField();
    }

    /**
     * 添加语言包
     */
    public function appBegin()
    {
        $lang = \request()->langset();
        $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
        //添加语言包
        Lang::load(ADDON_PATH . '/department/lang/' . $lang . '/' . $lang . '.php');

    }

    /**
     * 增加数据权限范围字段
     */
    private function addField()
    {

        $database = config('database');
        $isexits = Db::query("SHOW COLUMNS FROM {$database['prefix']}admin LIKE 'data_scope'");
        if (!$isexits) {
            Db::connect([], 'alter')->execute("ALTER TABLE {$database['prefix']}admin ADD `data_scope` tinyint(4)  NULL COMMENT '0默认1全部'");
        }
    }

    /**
     *
     */
    public function viewFilter(&$content)
    {
        $module = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        if ($module == 'admin' && $controller == 'index' && $action == 'login') {
            $third = get_addon_info('third');
			


            if ($third && $third['state'] == 1 && get_addon_config('department')['wechat_login']) {
                $uuid = \fast\Random::uuid();
                $url = url("department/third/qrcode") . "?uuid={$uuid}";
                $html = '<a class="btn btn-default btn-lg btn-block  btn-qrcodelogin " onclick="setInterval(qrcodelogin,\'2000\');"
            href=\'javascript:Layer.alert("<img src=\"' . $url . '\" style=\"max-height:250px;max-width:250px\"><p class=\"text-center text-qrcodelogin \">请用微信扫描登录。</p>");\'>
                <i class="fa fa-qrcode fa-fw"></i>扫码登录
            </a>';
                $content = preg_replace('/<!\-\-@(AdminLoginFormEnd)\-\->/i', "\$0\n" . $html, $content);

                $content = $content . "<script type='text/javascript'> 
                        function qrcodelogin(){
                            Fast.api.ajax({
                            type: 'GET',
                            url: 'department/third/login?uuid={$uuid}',
                            data: {},
                            loading:''
                        }, function (data,res) {
                            if(data){
                                location.href=data.url
                                return false;
                            }else{
                                $('.text-qrcodelogin').text(res.msg); return false;                
                            }
                        }, function (data,res) {
                            return false;
                        })};
                        </script>";


            }
        }
    }
}